<?php
/**
 * 
 */
class System_View_Root{
    /**
     * holds an instance of the registry
     *
     * @var Object
     * @access protected
     */
    protected $_registry;
    
    /**
     * Holds an instance of the class that is specific to the network
     * @access protected
     * @return mixed
     */
    protected $_network;
    
    /**
     * Class Constructor
     *
     * @access public
     * @return null
     */     
    public function __construct(){
        /**
         * get an instance of the registry
         * and set the network view object
         */
        $this->_registry    = System_Registry_Root::getInstance();        
    }
        
    /**
     * create the JavaScript head content for the current page
     *
     * @access private
     * @return String JavaScript links, and inline code
     */
    public function createJavaScript(){
        /**
         * create the links
         * if the links start with "http://" use that as the source,
         * else the source should start at the js folde
         */
        $links	= '';
        $files  = $this->_registry->getJavaScriptFiles();
		$path   = System_Registry_Configuration::get(System_Registry_Configuration::environment(), 'js', 'path');
        
        foreach($files as $file){
            $src = preg_match('/^http\:\/\//', $file) ? $file : $path . $file;
            
            $links .= Ele::tag('', 'script', 'type="text/javascript" src="'. $src .'"');
        }
        
        /**
         * create the load event js
         */
        $load_events    = System_Registry_Configuration::get('development', 'js', 'default_events');
        $events         = $this->_registry->getJavaScriptLoadEvents();
        
        foreach($events as $event){
            $load_events .= $event ."\n";
        }
        
        $dom_load = trim($load_events) == '' ? '' : "
            window.addEvent('domready', function(){
                ". $load_events ."
            });
        ";
         
        /**
         * create the inline js, including the load events
         */
        $inline_code    = $dom_load ."\n" . System_Registry_Configuration::get('development', 'js', 'default_inline') ."\n";
        $inline         = $this->_registry->getInlineJavaScript();
        
        foreach($inline as $code){
            $inline_code .= $code ."\n";
        }
        
        /**
         * return the complete JavaScript Code
         */
        return $links . "<script type=\"text/javascript\">
        /* <![CDATA[ */\n"
            .  $inline_code .
        "/* ]]> */
        </script>
        ";
    }
    
    /**
     * creates the css head content for the current page
     *
     * @access private
     * @return String, css links and inline code
     */
    public function createcss(){
        /**
         * create the css links
         */
        $links = '';
        $files = $this->_registry->getcssFiles();
		$path  = System_Registry_Configuration::get(System_Registry_Configuration::environment(), 'css', 'path');
        
        foreach($files as $file){
            $links .= "<link rel=\"stylesheet\" href=\"". $path . $file ."\" type=\"text/css\" media=\"screen\" />\n";
        }
        
        /**
         * links the css fix files for lovely IE!!
         */
        $links .= '
            <!--[if lt IE 8]>
                <link rel="stylesheet" href="/css/Ie6Fix.css" media="screen,projection" type="text/css" />
            <![endif]-->
            <!--[if IE 7]>
                <link rel="stylesheet" href="/css/Ie7Fix.css" media="screen,projection" type="text/css" />
            <![endif]-->
        ';
            
        /**
         * create inline css
         */
        $inline         = '';
        $css            = $this->_registry->getInlinecss();
        
        foreach($css as $code){
            $inline     .= $code ."\n";
        }
        
        $style          ="
        <style type=\"text/css\" media=\"screen\">
            ". $inline ."
        </style>"; 
        
        /**
         * return the complete css code
         */
        return $links . $style;
    }
    
    /**
     * Creates the Google Analytics
     *
     * @access private
     * @return String, Google Analytics code
     */
    private function googleAnalytics(){

    }
    
    /**
     * creates the page
     *
     * @access public
     * @return String, XHTML strict document
     */
    public function createPage(){
        /**
         * doctype
         * XHTML 1.0 Strict english
         */
        $doctype = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">' . "\n";
        
        /**
         * parse the head tags
         */
        $head_tags = '';
        if (count($this->_registry->getHeadTags())) {
            foreach ($this->_registry->getHeadTags() as $head_tag) {
                $head_tags .= $head_tag;
            }
        }
        
        /**
         * create the head
         * character set definition, title, js, and css
         */        
        $head = Ele::tag(
            Ele::eTag('meta', 'http-equiv="Content-Type" content="text/html; charset=utf-8"') .
            Ele::tag($this->_registry->getPageTitle(), 'title') .
            $this->createJavaScript() . 
            $this->createcss() . 
            $head_tags
        , 'head');
        
        /**
         * set the body
         * content, and google analytics code
         */
        $body = Ele::tag(
            $this->_network->buildPageContent() .
            $this->googleAnalytics()
        , 'body');
        
        /**
         * return the html wrapping all of the other content
         */
        return $doctype .
        Ele::tag(
            $head .
            $body
        ,'html', 'xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"');
    }
}

?>